﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using NHibernate.Criterion;
using Work.BL.Managers;
using Work.BL.POCOs;
using Work.DAL.Hibernate.Abstract;
using Work.DAL.Hibernate.Entities;
using Work.Tools.ObjectMapper;

namespace Work.BL.Hibernate.Managers
{
    public class HRegionManager : IRegionManager
    {
        #region Fields

        private readonly ICWSessionFactory _sessionFactory;
        private readonly IObjectMapper _objectMapper;

        #endregion

        #region Constructors

        public HRegionManager(ICWSessionFactory sessionFactory, IObjectMapper objectMapper)
        {
            this._sessionFactory = sessionFactory;
            this._objectMapper = objectMapper;
        }

        #endregion

        #region IProjectManager Members

        public List<RegionPOCO> GetRegions()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<Region> users = session.CreateCriteria<Region>().List<Region>();
                return users
                    .Select(r => this._objectMapper.Map<Region, RegionPOCO>(r))
                    .ToList();
            }
        }

        public RegionPOCO GetRegionById(int regionId)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Region region = session.Get<Region>(regionId);
                return _objectMapper.Map<Region, RegionPOCO>(region);
            }
        }

        public RegionPOCO Insert(RegionPOCO regionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Region region = _objectMapper.Map<RegionPOCO, Region>(regionPoco);
                session.Save(region);
                session.Flush();
                return _objectMapper.Map<Region, RegionPOCO>(region);
            }
        }

        public List<RegionPOCO> GetRegions(int startIndex, int maxCount)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                IList<Region> regions = session.CreateCriteria<Region>()
                    .SetFirstResult(startIndex)
                    .SetMaxResults(maxCount)
                    .List<Region>();
                List<RegionPOCO> list = regions
                    .Select(r => this._objectMapper.Map<Region, RegionPOCO>(r))
                    .ToList();
                return list;
            }
        }

        public int GetRegionCount()
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                int rowCount = session.CreateCriteria<Region>()
                    .SetProjection(Projections.RowCount()).UniqueResult<int>();
                return rowCount;
            }
        }

        public RegionPOCO Edit(RegionPOCO regionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Region region = _objectMapper.Map<RegionPOCO, Region>(regionPoco);
                session.Update(region);
                session.Flush();
                return _objectMapper.Map<Region, RegionPOCO>(region);
            }
        }

        public void Delete(RegionPOCO regionPoco)
        {
            using (ISession session = this._sessionFactory.CreateSession())
            {
                Region region = _objectMapper.Map<RegionPOCO, Region>(regionPoco);
                session.Delete(region);
                session.Flush();
            }
        }

        #endregion
        
    }
}
